package com.ruoyi.system.service.teacher.impl;

import com.ruoyi.system.domain.teacher.Statistics.TeacherStatisticsDTO;
import com.ruoyi.system.domain.teacher.Statistics.TeacherStatisticsRequestVO;
import com.ruoyi.system.mapper.award.competition.CompetitionAwardInfoMapper;
import com.ruoyi.system.mapper.award.research.ResearchAwardInfoMapper;
import com.ruoyi.system.mapper.award.teach.TeachAwardInfoMapper;
import com.ruoyi.system.mapper.honor.PersonalHonorMapper;
import com.ruoyi.system.mapper.project.BookMapper;
import com.ruoyi.system.mapper.project.PaperInfoMapper;
import com.ruoyi.system.mapper.project.PatentMapper;
import com.ruoyi.system.service.teacher.ITeacherStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * @Description:老师统计im pl
 * @Author: Liyujie
 * @CreateTime: 2021/11/20 16:09
 */
@Service
public class ITeacherStatisticsServiceImpl  implements ITeacherStatisticsService {
    @Autowired
    private ResearchAwardInfoMapper researchAwardInfoMapper;//科研获奖信息
    @Autowired
    private TeachAwardInfoMapper teachAwardInfoMapper;//教学获奖信息
    @Autowired
    private PersonalHonorMapper personalHonorMapper;//个人荣誉信息
    @Autowired
    private BookMapper bookMapper;//书籍信息
    @Autowired
    private PaperInfoMapper paperInfoMapper;//论文信息
    @Autowired
    private PatentMapper patentMapper;//专利信息
    @Autowired
    private CompetitionAwardInfoMapper competitionAwardInfoMapper;//竞赛获奖信息

    /**
     * 获取教师各项信息（带Redis缓存）
     *
     * @param requestVO 包含用户ID和年份的查询参数
     * @return 统计结果DTO
     * @Cacheable：表示方法结果会被缓存，下次相同参数调用时直接返回缓存数据
     * value：缓存名称，用于区分不同缓存区域
     * key：缓存键，使用用户 ID + 年份确保唯一性
     * unless：条件判断，结果为 null 时不缓存
     */
    @Override
  //  @Cacheable(value = "teacherStatistics_getIndexInfo", key = "#requestVO.userId + '-' + #requestVO.year", unless = "#result == null")
    public TeacherStatisticsDTO getIndexInfo(TeacherStatisticsRequestVO requestVO) {
        // 1. 从请求参数中获取用户ID和年份
        String userId = requestVO.getUserId();
        String year = requestVO.getYear();

        int researchAwardCount = 0;//科研获奖信息//如果指导老师里有他就算
        int personalHonorCount = 0;//个人荣誉信息
        int bookCount =0;//书籍信息//如果指导老师里有他就算
        int paperCount =0;//论文信息//如果指导老师里有他就算
        int teachAwardCount = 0;//教学获奖信息//如果指导老师里有他就算
        //如果指导老师里有他就算
        int competitionCount=0;//竞赛获奖信息
        //int patentCount = patentMapper.countByUserIdAndYear(userId, year);//专利信息
        if(year!=null&&year.equals("before2020")){
            researchAwardCount = researchAwardInfoMapper.countByUserIdAndYearBefore(userId );//科研获奖信息//如果指导老师里有他就算
            personalHonorCount = personalHonorMapper.countByUserIdAndYearBefore(userId );//个人荣誉信息
            bookCount = bookMapper.countByUserIdAndYearBefore(userId );//书籍信息//如果指导老师里有他就算
            paperCount = paperInfoMapper.countByUserIdAndYearBefore(userId );//论文信息//如果指导老师里有他就算
            teachAwardCount = teachAwardInfoMapper.countByUserIdAndYearBefore(userId );//教学获奖信息//如果指导老师里有他就算
            //如果指导老师里有他就算
            competitionCount=competitionAwardInfoMapper.countByUserIdAndYearBefore(userId );//竞赛获奖信息
        }else {
             researchAwardCount = researchAwardInfoMapper.countByUserIdAndYear(userId, year);//科研获奖信息//如果指导老师里有他就算
             personalHonorCount = personalHonorMapper.countByUserIdAndYear(userId, year);//个人荣誉信息
             bookCount = bookMapper.countByUserIdAndYear(userId, year);//书籍信息//如果指导老师里有他就算
             paperCount = paperInfoMapper.countByUserIdAndYear(userId, year);//论文信息//如果指导老师里有他就算
             teachAwardCount = teachAwardInfoMapper.countByUserIdAndYear(userId, year);//教学获奖信息//如果指导老师里有他就算
            //如果指导老师里有他就算
             competitionCount=competitionAwardInfoMapper.countByUserIdAndYear(userId, year);//竞赛获奖信息
            //int patentCount = patentMapper.countByUserIdAndYear(userId, year);//专利信息
        }
        int patentCount = 0;

        // 3. 组装统计结果DTO
        TeacherStatisticsDTO statisticsDTO = new TeacherStatisticsDTO();
        statisticsDTO.setUserId(userId);
        statisticsDTO.setYear(year);
        statisticsDTO.setResearchAwardCount(researchAwardCount);
        statisticsDTO.setTeachAwardCount(teachAwardCount);
        statisticsDTO.setPersonalHonorCount(personalHonorCount);
        statisticsDTO.setBookCount(bookCount);
        statisticsDTO.setPaperCount(paperCount);
        statisticsDTO.setPatentCount(patentCount);
        statisticsDTO.setCompetitionCount(competitionCount);
        return statisticsDTO;
    }
}
